package ie.nolantech.gpsg;

import ie.nolantech.util.BetterLocation;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.os.SystemClock;
import android.util.Log;
import android.widget.RemoteViews;

public class GpsLoggerService extends Service {

	private static final String TAG = "GpsLoggerService";
	private final IBinder mBinder = new LocalBinder();
	
	final static int ONGOING_NOTIFICATION = 1001;

	GpsHelper mHelper;
	LocationManager locationManager;
	LocationListener locationListener;
	LocationListener networkLocationListener;
	Location currentLocation;
	Notification mNotification;
	boolean serviceActive;

	long startTime;
	long elapsed = 0;

	@Override
	public IBinder onBind(Intent intent) {
		Log.d(TAG, "onBind");

		return mBinder;
	}

	public class LocalBinder extends Binder {
		GpsLoggerService getService() {
			return GpsLoggerService.this;
		}
	}

	public long kickOff() {

		startTime = SystemClock.elapsedRealtime();
		serviceActive = true;

		RemoteViews contentView = new RemoteViews(getPackageName(),
				R.layout.notification_layout);

		contentView.setChronometer(R.id.chronometer1,
				SystemClock.elapsedRealtime(), null, true);

		mNotification = new Notification(R.drawable.icon,
				getText(R.string.app_name), System.currentTimeMillis());
		
		Intent notificationIntent = new Intent(GpsLoggerService.this, GpsGameActivity.class);
		
		PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
				notificationIntent, 0);
		mNotification.setLatestEventInfo(this, null, null, pendingIntent);

		mNotification.contentView = contentView;

		startForeground(ONGOING_NOTIFICATION, mNotification);

		mHelper = new GpsHelper(this);

		locationManager = (LocationManager) this
				.getSystemService(Context.LOCATION_SERVICE);

		locationListener = new LocationListener() {
			public void onLocationChanged(Location location) {
				mHelper.insertPosition(location.getLatitude(),
						location.getLongitude(), location.getSpeed(), "GPS"
								+ (setCurrentLocation(location) ? "Better"
										: "Worse"));
			}

			public void onStatusChanged(String provider, int status,
					Bundle extras) {
			}

			public void onProviderEnabled(String provider) {
			}

			public void onProviderDisabled(String provider) {
			}
		};

		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
				0, locationListener);

		serviceActive = true;

		return elapsed;
	}

	public void finalWhistle() {
		locationManager.removeUpdates(locationListener);
		stopForeground(true);
		serviceActive = false;
	}

	public boolean isServiceActive() {
		return serviceActive;
	}

	private boolean setCurrentLocation(Location newLocation) {
		BetterLocation bl = new BetterLocation();
		if (bl.isBetterLocation(newLocation, currentLocation)) {
			currentLocation = newLocation;
			return true;
		} else {
			return false;
		}
	}
}
